﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iSun.Common;


namespace iSun.BLL
{
    /// <summary>
    /// 部門的業務邏輯類
    /// 江怀玉
    /// iSunStudio@hotmail.com
    /// 日期:2012-09-26
    /// </summary>
    public class BDeptView: Base.BaseBussnies
    {
        public IList<iSun.Entity.Department> getTreeView(Guid pid, string table)
        {
            iSun.Entity.Department treeView = GetEntityBySqlComm<iSun.Entity.Department>(string.Format("select * from [{0}] where {2}='{1}'", table, pid, pid == Guid.Empty ? "ParentID" : "ID"));
            IList<iSun.Entity.Department> list = this.GetEntitysBySqlComm<iSun.Entity.Department>(string.Format("select * from [{0}] where PathCode like '{1}%' order by pathcode", table, treeView.PathCode));
            return list;
        }

        /// <summary>
        /// 獲取部門樹數據
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public IList<iSun.Entity.Department> getDeptTreeView(Guid pid)
        {
            return getTreeView(pid, "SysDept");
        }



        /// <summary>
        /// 将树数据,转换成JSON数据格式
        /// </summary>
        /// <param name="list">部門列表</param>
        /// <returns>回傳JSON數據格式</returns>
        public string ConvertToDojoJSON(IList<iSun.Entity.Department> list)
        {
            StringBuilder _DojoJson = new StringBuilder(list != null ? @"{identifier: ""ID"",label: ""Name"",items: [{0}]}" :@"{identifier: ""ID"",label:""Name"",items: []}");
            if (list == null)
            {
                return _DojoJson.ToString();
            }
            StringBuilder _sb = new StringBuilder("");
            var _treeView = list.OrderBy(p => p.PathCode).First();
            {
                string _dot = _sb.ToString() == string.Empty ? "" : ",";
                string _dojoJson = ConvertToDojoJSON(_treeView, list);
                if (_dojoJson != string.Empty)
                {
                    _sb.Append(_dot + _dojoJson);
                }
            }
            return _DojoJson.ToString().Replace("{0}", _sb.ToString());
        }

      //{id: 'AF', name:'Africa',
      //  children:[
      //  {id: 'EG', name:'Egypt' },
      //  {id: 'KE', name:'Kenya',
      //      children:[
      //      {id: 'Nairobi', name:'Nairobi', type:'city' },
      //      {id: 'Mombasa', name:'Mombasa', type:'city' }
      //    ]
      //  },
     


        private bool hasChlild(iSun.Entity.Department treeView, IList<iSun.Entity.Department> list)
        {
            return list.Where(p => p.ParentID == treeView.ID).Count() > 0;
        }

        private string ConvertToDojoJSON(iSun.Entity.Department treeView, IList<iSun.Entity.Department> list)
        {
            string json = "";
            if (hasChlild(treeView, list))
            {
                json = iSun.Common.Globals.GetTreeViewJSON(treeView);
                json = json.Remove(json.Length - 1, 1) + ",children:[{0}]}";
                var _list = list.Where(p => p.ParentID == treeView.ID);
                //string _child = "_reference:'{0}'";
                StringBuilder _sb = new StringBuilder("");
                foreach (var _treeView in _list)
                {
                    string _dot = _sb.ToString() == string.Empty ? "" : ",";
                    //string _dot = ",";
                    if (hasChlild(_treeView, list))
                    {
                        _sb.Append(_dot+ConvertToDojoJSON(_treeView,list));
                    }
                    else
                    {
                        //string _dot = _sb.ToString() == string.Empty ? "" : ",";
                        _sb.Append(_dot + _treeView.ToJSON());
                    }
                }
                json = json.Replace("{0}", _sb.ToString());
            }
            else
            {
                json = iSun.Common.Globals.GetTreeViewJSON(treeView);
            }
            return json;
        }

    }
}
